package com.mentalhealthplatform.global.redisGuard;

import com.mentalhealthplatform.appModels.*;
import com.mentalhealthplatform.global.rbac.model.User;
import com.mentalhealthplatform.studentV3.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.UUID;

/**
 * @author Pear
 */
@Service
public class DatabaseService {
    @Autowired
    private BaseUserRepositoryV3 baseUserRepositoryV3;

    @Autowired
    private AssessmentRepositoryV3 assessmentRepositoryV3;

    @Autowired
    private BookingLectureRepositoryV3 bookingLectureRepositoryV3;

    @Autowired
    private DetailRepositoryV3 detailRepositoryV3;

    @Autowired
    private LectureRepositoryV3 lectureRepositoryV3;

    @Autowired
    private MessageRepositoryV3 messageRepositoryV3;

    @Autowired
    private ReplyRepositoryV3 replyRepositoryV3;

    @Autowired
    private StudentRepositoryV3 studentRepositoryV3;

    @Autowired
    private TestRepositoryV3 testRepositoryV3;

    @Autowired
    private TweetRepositoryV3 tweetRepositoryV3;

    @Autowired
    private CarouselItemRepository carouselItemRepository;

    public <T> T getDataFromDatabase(String key, Class<T> type) {
        if (type.equals(User.class)) {
            Optional<User> user = baseUserRepositoryV3.findByUsername(key);
            return type.cast(user.orElse(null));
        } else if (type.equals(Assessment.class)) {
            //根据ID查找Student的方法
            Optional<Student> studentOpt = studentRepositoryV3.findById(UUID.fromString(key));
            // 或者抛出异常，具体情况具体处理
            return studentOpt.map(student -> type.cast(assessmentRepositoryV3.findByStudentId(student))).orElse(null);
        } else if (type.equals(BookingLecture.class)) {
            // 根据实际参数调整
            return type.cast(bookingLectureRepositoryV3.findByPhone(key));
        } else if (type.equals(Detail.class)) {
            return type.cast(detailRepositoryV3.findByType(key));
        } else if (type.equals(Lecture.class)) {
            // key是Lecture的ID
            return type.cast(lectureRepositoryV3.findById(Long.valueOf(key)));
        } else if (type.equals(Message.class)) {
            // key是UUID格式的学生ID
            return type.cast(messageRepositoryV3.findByStudentId(UUID.fromString(key)));
        } else if (type.equals(Reply.class)) {
            // key是Reply的ID
            return type.cast(replyRepositoryV3.findById(Long.valueOf(key)));
        } else if (type.equals(Student.class)) {
            // key是UUID格式的学生ID
            return type.cast(studentRepositoryV3.findById(UUID.fromString(key)));
        } else if (type.equals(Test.class)) {
            // key是Test的ID
            return type.cast(testRepositoryV3.findById(Integer.valueOf(key)));
        } else if (type.equals(Tweet.class)) {
            // key是Tweet的ID
            return type.cast(tweetRepositoryV3.findById(Long.valueOf(key)));
        } else if (type.equals(CarouselItem.class)) {
            // key是CarouselItem的ID
            return type.cast(carouselItemRepository.findById(Long.valueOf(key)));
        }

        return null;
    }
}
